package com.artlongs.webapp.controller;

import com.artlongs.base.controller.BaseController;
import com.artlongs.common.Enums;
import com.artlongs.webapp.user.model.User;
import com.artlongs.webapp.user.service.UserService;
import com.artlongs.base.vo.BizRetCode;
import com.artlongs.base.vo.BizRetVo;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Function:用户
 * ========================================
 * autor: lqf
 * Date: 14-4-6
 * ========================================
 */

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Resource(name="userServiceImpl")
    private UserService userService;



    @RequestMapping("/list")
    public String index(Model model){

        model.addAttribute("message", "HELLO LITON!");
        Iterable<User> userList = userService.getAllUsers();
        if (!userList.iterator().hasNext()) {
            userService.makeSomeUsers();
        }
        model.addAttribute("userList", userList);

        model.addAttribute("show", "I'm is linton.");


        return "/user/list";
    }

    @RequestMapping("/edit/{userId}")
    public String edit(Model model,@PathVariable Long userId) {

        User user = userService.get(userId);
        model.addAttribute("user", user);
        model.addAttribute("bloodtype",   Enums.toList(Enums.BloodType.class));

        return "/user/edit";
    }

    @RequestMapping("/edit/post")
    @ResponseBody
    public BizRetVo editPost(User user ) {
        BizRetVo bizRetVo = userService.update(user);
        return bizRetVo;
    }

}
